
import { Application } from 'egg';
import { MEDIUMINT, TEXT, STRING, INTEGER, BOOLEAN, DATE, Instance } from 'sequelize';
import { IUserInst } from './user'

export interface IAricleAttr {
  id?: number;
  authorId: number;
  content: string;
  title?: string;
  category?: string;
  abstract?: string; // 简介
  img?: string;    // 封面
  source?: string; // 文章来源(出处)
  status?: number;
  tags?: string;
  allowComment?: boolean;
  top?: boolean;
  good?: boolean;
  draftTime?: Date;
  lastCommentTime?: Date;
  commentCount?: number;
  collectionCount?: number;
  likeCount?: number;
  pvCount?: number;
  likeUsers?: IUserInst[];
}

export interface IArticleInst extends Instance<IAricleAttr>, IAricleAttr {
  id: number;
  authorId: number;
  content: string;
  title: string;
  category: string;
  abstract: string; // 简介
  img: string;    // 封面
  source: string; // 文章来源(出处)
  status: number;
  tags: string;
  allowComment: boolean;
  top: boolean;
  good: boolean;
  draftTime: Date;
  lastCommentTime: Date;
  commentCount: number;
  collectionCount: number;
  likeCount: number;
  pvCount: number;
  likeUsers: IUserInst[];
}

export default (app: Application) => {
  const model = app.model.define<IArticleInst, IAricleAttr>('article', {
    id: {
      type: MEDIUMINT(8).UNSIGNED,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
    authorId: {
      type: MEDIUMINT(8).UNSIGNED,
      allowNull: false,
    },
    title: {
      type: STRING,
      defaultValue: '',
    },
    category: {         // 类型分类
      type: STRING,
      defaultValue: '',
    },
    abstract: {
      type: STRING,
      defaultValue: '',
    },
    img: {              // 封面图
      type: STRING,
      defaultValue: '',
    },
    source: {           // 来源
      type: STRING,
      defaultValue: '',
    },
    content: {
      type: TEXT,
      allowNull: false,
    },
    status: {
      type: INTEGER(10),
      allowNull: false,
      defaultValue: 1,  // 0 表示删除，1 表示有效
    },
    tags: {
      type: STRING,
      defaultValue: '',
    },
    allowComment: {    // 允许评论
      type: BOOLEAN,
      defaultValue: true,
    },
    top: { // 置顶文章
      type: BOOLEAN,
      defaultValue: false,
    },
    good: { // 精华文章
      type: BOOLEAN,
      defaultValue: false,
    },
    draftTime: { // 定为草稿的时间
      type: DATE,
    },
    lastCommentTime: { // 最后一次评论时间
      type: DATE,
    },
    commentCount: { // 评论数
      type: INTEGER(10),
      defaultValue: 0,
    },
    collectionCount: { // 收藏数
      type: INTEGER(10),
      defaultValue: 0,
    },
    likeCount: { // 点赞数
      type: INTEGER(10),
      defaultValue: 0,
    },
    pvCount: { // 浏览量
      type: INTEGER(10),
      defaultValue: 0,
    },
  }, {
    timestamps: true,
    paranoid: false,
    charset: 'utf8mb4',
    defaultScope: {
      where: {
        status: 1,
      },
    },
    scopes: {
      deleted: {
        where: {
          status: 0,
        },
      },
      all: {
        where: {
        },
      },
    },
  });

  model.associate = () => {
    app.model.Article.belongsTo(app.model.User, { as: 'author', foreignKey: 'authorId' });
  };

  // model.sync({ alter: true });
  // model.sync({force: true}); //强制
  return model;
};
